#include "EpilLocalVariableStatementInterpreter.h"
#include "EpilInterpreterException.h"
#include "EpilExpressionInterpreter.h"
#include "EpilAssignOperationHelper.h"
#include "EpilInterpreter.h"
#include "EpilExpressionListInterpreter.h"


EpilLocalVariableStatementInterpreter::EpilLocalVariableStatementInterpreter( EpilInterpreter * interpreter )
: EpilInterpreterBase( interpreter )
{
}


void EpilLocalVariableStatementInterpreter::Run( const EpilLocalVariableStatementCode * code )
{
	const list<tstring> & assignees = code->GetVariables();
	const EpilExpressionCodeList & assigners = code->GetAssigners();

	EpilExpressionListInterpreter eli( this->GetInterpreter() );
	eli.Run( assigners );
	EpilObjectList objects = eli.GetObjects();

	if( assignees.size() > objects.size() )
	{
		size_t extraNilCount = assignees.size() - objects.size();
		for( size_t i = 0; i < extraNilCount; i++ )
		{
			objects.push_back( this->GetInterpreter()->CreateNilObject() );
		}
	}

	list<tstring>::const_iterator itAssignee = assignees.begin();
	EpilObjectList::const_iterator itAssigner = objects.begin();
	for( size_t i = 0; i < assignees.size(); i++ )
	{
		EpilAssignOperationHelper::AssignLocal( this->GetInterpreter(), *itAssignee, *itAssigner );
		++itAssignee;
		++itAssigner;
	}
}